import { validateCustomPositiveInt, validateCustomTwoDecimal } from '@/utils/validate'
export default {
  data() {
    //  参与交易类型
    // const validateBizType = (rule, value, callback) => {
    //   if (this.form.bizTypeRadioExtra === 2 && value.length === 0) {
    //     callback(new Error("请至少设置一个参与的交易类型"));
    //   } else {
    //     callback();
    //   }
    // };
    // //  参与支付方式
    // const validatePayType = (rule, value, callback) => {
    //   if (this.form.payTypeRadioExtra === 2 && value.length === 0) {
    //     callback(new Error("请至少设置一种支付方式"));
    //   } else {
    //     callback();
    //   }
    // };

    //  校验优惠券id
    // const validateCouponId = (rule, value, callback) => {
    //   if (value === '') {
    //     callback('请选择优惠券')
    //     // } else if (this.invalidCouponIds.includes(value)) {
    //     //   callback(new Error("该券已过期或已删除，请更换其它券"));
    //   } else {
    //     callback()
    //   }
    // }

    // 优惠券
    const validateGoodsItems = (rule, value, callback) => {
      if (!value.length) {
        callback('请至少选择一种')
      } else if (value.length > 10) {
        callback('卡券种类不超过10种')
      } else {
        let maxAmount = 0
        value.forEach(v => {
          if (validateCustomPositiveInt(v.num, 2)) {
            maxAmount += v.num
          }
        })
        if (maxAmount > 30) {
          callback('卡券总数量不超过30张')
        } else {
          callback()
        }
      }
    }

    // 优惠券数量
    const validateCouponNum = (rule, value, callback) => {
      if (this.$isNull(value)) {
        callback('请输入数量')
      } else if (!validateCustomPositiveInt(value, 2)) {
        callback(new Error('请输入正整数'))
      } else {
        this.$refs.form.validateField(['goodsItems'])
        callback()
      }
    }

    // 属性
    const validateSpecList = (rule, value, callback) => {
      if (value && value.length > 3) {
        callback(new Error('最多设置三种销售属性'))
      } else {
        this.$refs.form.clearValidate(['specList'])
        callback()
      }
    }

    // 价格管理
    const validatorPrice = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入价格'))
      } else if (!validateCustomTwoDecimal(value, 7)) {
        callback(new Error('范围0.01~9999999.99'))
      } else {
        callback()
      }
    }

    // 价格管理
    const validatorOriginPrice = (rule, value, callback) => {
      if (!value) {
        callback()
      } else if (!validateCustomTwoDecimal(value, 7)) {
        callback(new Error('范围0.01~9999999.99'))
      } else {
        callback()
      }
    }

    // 限制数量
    const validateGoodsDelivery = (rule, value, callback) => {
      if (this.form.goodsType === 100) {
        if (value.canExpress || value.canLocalDelivery || value.canSelfPickup) {
          this.$refs.form.clearValidate(['goodsDelivery'])
          callback()
        } else {
          callback(new Error('请至少选择一种配送方式'))
        }
      } else {
        callback()
      }
    }
    // 备货天数
    const validateDeliveryPrepare = (rule, value, callback) => {
      if (!value) {
        callback('请输入')
      } else if (!validateCustomPositiveInt(value, 3)) {
        callback(new Error('请输入正整数'))
      } else {
        callback()
      }
    }

    // 限制数量
    const validateLimitNum = (rule, value, callback) => {
      if (this.form.goodsRule.limited) {
        if (!value) {
          callback('请输入数量')
        } else if (!validateCustomPositiveInt(value, 2, [100])) {
          callback(new Error('请输入1~100正整数'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }

    // 指定会员
    const validateCardIdList = (rule, value, callback) => {
      if (this.form.goodsRule.memberType == 2) {
        if (value && value.length === 0) {
          callback('请选择购买对象')
        } else {
          callback()
        }
      } else {
        callback()
      }
    }

    // 定金
    const validateDepositQuota = (rule, value, callback) => {
      if (this.$isNull(value)) {
        callback(new Error('请输入'))
      } else if (this.form.goodsPresell.depositType === 0) {
        // 固定定金
        if (!validateCustomTwoDecimal(value, 7)) {
          callback('请输入0.01~999999.99')
        } else {
          callback()
        }
      } else if (this.form.goodsPresell.depositType === 1) {
        // 比例定金
        if (!validateCustomPositiveInt(value, 2, [100])) {
          callback('请输入比例1~100')
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    // 发货天
    const validate1To999 = (rule, value, callback) => {
      if (this.$isNull(value)) {
        callback(new Error('请输入'))
      } else if (!validateCustomPositiveInt(value, 3)) {
        callback(new Error('请输入正整数'))
      } else {
        callback()
      }
    }

    // 违约金
    const validateDefaultFine = (rule, value, callback) => {
      if (this.$isNull(value)) {
        callback(new Error('请输入'))
      } else if (this.form.goodsPresell.defaultFineType === 0) {
        // 固定定金
        if (!validateCustomTwoDecimal(value, 7)) {
          callback('请输入0.01~999999.99')
        } else {
          callback()
        }
      } else if (this.form.goodsPresell.defaultFineType === 1) {
        // 比例定金
        if (!validateCustomPositiveInt(value, 2, [100])) {
          callback('请输入比例1~100')
        } else {
          callback()
        }
      } else {
        callback()
      }
    }

    // 退订
    const validateCancelCharge = (rule, value, callback) => {
      if (this.$isNull(value)) {
        callback(new Error('请输入'))
      } else if (this.form.goodsPresell.cancelChargeType === 0) {
        // 固定定金
        if (!validateCustomTwoDecimal(value, 7)) {
          callback('请输入0.01~999999.99')
        } else {
          callback()
        }
      } else if (this.form.goodsPresell.cancelChargeType === 1) {
        // 比例定金
        if (!validateCustomPositiveInt(value, 2, [100])) {
          callback('请输入比例1~100')
        } else {
          callback()
        }
      } else {
        callback()
      }
    }

    // 配件长度
    const validateGoodsBargains = (rule, value, callback) => {
      if (value.length > 10) {
        callback(new Error('配件最多可添加十种'))
      } else {
        callback()
      }
    }

    // 配件限购
    const validateGoodsBargainsLimitNum = (rule, value, callback) => {
      console.log(value)
      if (value.leastNum && value.perLimitNum && value.perLimitNum < value.leastNum) {
        callback(new Error(rule.valueType === 'leastNum' ? '应小于等于最多选购数量' : '应大于等于最少选购数量'))
      } else {
        this.$refs.form.clearValidate([rule.field])
        callback()
      }
    }
    return {
      //  表单校验字段相关
      rules: {
        goodsType: [
          {
            required: true,
            message: '请选择商品类型',
            trigger: ['blur', 'change']
          }
        ],
        goodsItems: [
          {
            type: 'array',
            required: true,
            validator: validateGoodsItems,
            trigger: ['blur', 'change']
          }
        ],
        // couponId: [
        //   {
        //     required: true,
        //     validator: validateCouponId,
        //     trigger: ['blur', 'change']
        //   }
        // ],
        couponNum: [
          {
            required: true,
            validator: validateCouponNum,
            trigger: ['blur', 'change']
          }
        ],
        categoryIds: [
          {
            required: true,
            message: '请选择商品类目',
            trigger: ['blur', 'change']
          }
        ],
        title: [
          {
            required: true,
            message: '请输入商品标题',
            trigger: ['blur', 'change']
          }
        ],
        specList: [
          {
            validator: validateSpecList,
            trigger: ['blur', 'change']
          }
        ],
        attrList: [
          {
            type: 'array',
            required: true,
            message: '请至少设置一种属性选项'
            // trigger: "change"
          }
        ],
        price: [
          {
            required: true,
            validator: validatorPrice,
            trigger: ['blur', 'change']
          }
        ],
        originPrice: [
          {
            validator: validatorOriginPrice,
            trigger: ['blur', 'change']
          }
        ],
        paramsName: [
          {
            required: true,
            message: '请输入参数名',
            trigger: ['blur', 'change']
          }
        ],
        paramsValue: [
          {
            required: true,
            message: '请输入参数内容',
            trigger: ['blur', 'change']
          }
        ],
        goodsDelivery: [
          {
            required: true,
            validator: validateGoodsDelivery,
            trigger: ['blur', 'change']
          }
        ],
        deliveryPrepare: {
          required: true,
          validator: validateDeliveryPrepare,
          trigger: ['blur', 'change']
        },
        limitNum: [
          {
            type: 'array',
            required: true,
            validator: validateLimitNum,
            trigger: ['blur', 'change']
          }
        ],
        cardIdList: [
          {
            type: 'array',
            required: true,
            validator: validateCardIdList,
            trigger: ['blur', 'change']
          }
        ],
        presell: [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.type': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.depositType': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.depositQuota': [
          {
            required: true,
            validator: validateDepositQuota,
            trigger: ['blur', 'change']
          }
        ],
        'goodsPresell.limitTime': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.preDate': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.afterTheEnd': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.remainingBeginTime': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.remainingEndTime': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.defaultRule': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.defaultFineType': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.defaultFine': [
          {
            required: true,
            validator: validateDefaultFine,
            trigger: ['blur', 'change']
          }
        ],
        'goodsPresell.prepareType': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.prepareTime': [
          {
            required: true,
            validator: validate1To999,
            trigger: ['blur', 'change']
          }
        ],
        'goodsPresell.prepareUnit': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.prepareBeginTime': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.cancelRule': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.cancelFreeTime': [
          {
            required: true,
            validator: validate1To999,
            trigger: ['blur', 'change']
          }
        ],
        'goodsPresell.cancelChargeType': [
          {
            required: true,
            message: '请选择'
          }
        ],
        'goodsPresell.cancelCharge': [
          {
            required: true,
            validator: validateCancelCharge,
            trigger: ['blur', 'change']
          }
        ],
        // imageList: [
        //   {
        //     type: 'array',
        //     required: true,
        //     message: '请上传商品图片',
        //     trigger: ['blur', 'change']
        //   }
        // ],
        goodsBargains: [
          {
            validator: validateGoodsBargains,
            trigger: ['blur', 'change']
          }
        ],
        goodsBargainsLeastNum: [
          {
            required: true,
            validator: validateGoodsBargainsLimitNum,
            valueType: 'leastNum',
            trigger: ['blur', 'change']
          }
        ],
        goodsBargainsPerLimitNum: [
          {
            required: true,
            validator: validateGoodsBargainsLimitNum,
            valueType: 'perLimitNum',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  watch: {
    'form.goodsType'(val) {
      this.$refs.form.clearValidate()
    },
    'form.goodsRule.memberType'(val) {
      this.$refs.form.clearValidate(['goodsRule.cardIdList'])
    },
    'form.goodsRule.limited'(val) {
      if (!val) {
        this.$refs.form.clearValidate(['goodsRule.limitNum'])
      }
    },
    'form.goodsPresell.depositType'(val) {
      this.$refs.form.clearValidate(['goodsPresell.depositQuota'])
    },
    'form.goodsPresell.cancelChargeType'(val) {
      this.$refs.form.clearValidate(['goodsPresell.cancelCharge'])
    }
    // form: {
    //   handler: function (val) {
    //     this.$nextTick(() => {

    //     });
    //   },
    //   deep: true
    // }
  },
  methods: {
  }
}
